package com.cskaoyan.service;

import com.cskaoyan.bean.Issue;
import com.cskaoyan.bean.IssueExample;
import com.cskaoyan.bean.Keyword;
import com.cskaoyan.bean.KeywordExample;
import com.cskaoyan.bo.*;
import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author LiaoLong
 * @date 2021-05-09 11:28
 */
@Service
public class MallManagementServiceImpl implements MallManagementService {
    @Autowired
    private KeywordMapper keywordMapper;

    @Autowired
    private IssueMapper issueMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Autowired
    private ShowRegionMapper showRegionMapper;

    @Autowired
    private BrandInfoMapper brandInfoMapper;

    /**
     * 获取关键词列表
     *
     * @param searchKeywordBO
     * @return
     */
    @Override
    public KeywordVO getKeywords(SearchKeywordBO searchKeywordBO) {
        PageHelper.startPage(searchKeywordBO.getPage(), searchKeywordBO.getLimit());

        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(searchKeywordBO.getSort() + " " + searchKeywordBO.getOrder());
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(searchKeywordBO.getKeyword())) {
            criteria.andKeywordLike("%" + searchKeywordBO.getKeyword() + "%");
        }
        if (Objects.nonNull(searchKeywordBO.getUrl())) {
            criteria.andUrlLike("%" + searchKeywordBO.getUrl() + "%");
        }

        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        long total = keywordPageInfo.getTotal();

        KeywordVO keywordVO = new KeywordVO();
        keywordVO.setItems(keywords);
        keywordVO.setTotal(total);
        return keywordVO;
    }

    /**
     * 逻辑删除或更改一条关键词
     *
     * @param keyword
     * @param op
     * @throws ArithmeticException
     */
    @Transactional
    @Override
    public void updateKeyword(Keyword keyword, String op) throws ArithmeticException {

        if ("delete".equals(op)) {
            keyword.setDeleted(true);
        }
        KeywordExample keywordExample = new KeywordExample();
        KeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andIdEqualTo(keyword.getId());

        keyword.setUpdateTime(new Date());

        // TODO: 编写异常处理
        int count = keywordMapper.updateByExampleSelective(keyword, keywordExample);
        try {
            // int i = 1/0;
        } catch (ArithmeticException e) {
            throw e;
        }
    }

    /**
     * 添加一条关键词
     *
     * @param keyword
     */
    @Transactional
    @Override
    public void createKeyword(Keyword keyword) {
        // TODO: 异常处理
        Date date = new Date();
        keyword.setAddTime(date);
        keyword.setUpdateTime(date);
        int count = keywordMapper.insertSelective(keyword);
    }

    /**
     * 获取通用问题列表
     *
     * @param searchIssueBO
     * @return
     */
    @Override
    public IssueVO getIssue(SearchIssueBO searchIssueBO) {
        //分页
        PageHelper.startPage(searchIssueBO.getPage(), searchIssueBO.getLimit());

        IssueExample issueExample = new IssueExample();

        //搜索结果排序
        issueExample.setOrderByClause(searchIssueBO.getSort() + " " + searchIssueBO.getOrder());

        //添加搜索条件
        IssueExample.Criteria criteria = issueExample.createCriteria();
        //判断是否已逻辑删除：deleted:true
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull((searchIssueBO.getQuestion()))) {
            criteria.andQuestionLike("%" + searchIssueBO.getQuestion() + "%");
        }

        //查询
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        Long total = issuePageInfo.getTotal();

        IssueVO issueVO = new IssueVO();
        issueVO.setItems(issues);
        issueVO.setTotal(total);
        return issueVO;
    }

    /**
     * 编辑问题
     *
     * @param issue
     */
    @Override
    public void updateIssue(Issue issue) {

        issue.setUpdateTime(new Date());
        int count = issueMapper.updateByPrimaryKey(issue);
    }

    /**
     * 删除问题：逻辑删除
     *
     * @param issue
     */
    @Override
    public void deleteIssue(Issue issue) {
        Date date = new Date();
        issue.setDeleted(true);
        issue.setUpdateTime(date);
        int count = issueMapper.updateByPrimaryKey(issue);
    }

    /**
     * 新增问题
     *
     * @param issue
     */
    @Override
    public void createIssue(Issue issue) {

        Date date = new Date();
        issue.setAddTime(date);
        issue.setUpdateTime(date);
        issue.setDeleted(false);
        int count = issueMapper.insertSelective(issue);
    }



    /** 订单列表
     * @return
     */
    @Override
    public OrderVo query(OrderBO orderBO) {
        PageHelper.startPage(orderBO.getPage(),orderBO.getLimit());
        OrderExample orderExample = new OrderExample();
        orderExample.setOrderByClause(orderBO.getSort()+" "+orderBO.getOrder());
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andDeletedNotEqualTo(true);
        if (Objects.nonNull(orderBO.getOrderSn())){
            criteria.andOrderSnLike("%"+orderBO.getOrderSn()+"%");
        }
        if (Objects.nonNull(orderBO.getOrderStatusArray())){
            criteria.andOrderStatusEqualTo(orderBO.getOrderStatusArray());
        }
        if (Objects.nonNull(orderBO.getUserId())){
            criteria.andUserIdEqualTo(orderBO.getUserId());
        }
        List<Order> orders = orderMapper.selectByExample(orderExample);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        long total = orderPageInfo.getTotal();

        OrderVo orderVo = new OrderVo();
        orderVo.setItems(orders);
        orderVo.setTotal(total);

        return orderVo;
    }

    /**
     * 订单详情
     * @param id
     * @return
     */
    @Override
    public OrderListVo selectDetail(Integer id) {
        Order order = orderMapper.selectByPrimaryKey(id);
        Integer userId = order.getUserId();

        User user = userMapper.selectByPrimaryKey(userId);

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        OrderListVo orderListVo = new OrderListVo();
        orderListVo.setUser(user);
        orderListVo.setOrder(order);
        orderListVo.setOrderGoods(orderGoods);

        return orderListVo;
    }

    /**
     * 订单发货
     * @param orderShipBO
     */
    @Override
    public void updateOrderFund(OrderShipBO orderShipBO) {
        int orderId = orderShipBO.getOrderId();
        String shipChannel = orderShipBO.getShipChannel();
        String shipSn = orderShipBO.getShipSn();

        Order order = new Order();
        order.setId(orderId);
        order.setOrderStatus(301);
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);
        //跟新时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        dateFormat.format(date);
        order.setUpdateTime(date);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public List<ShowRegionVO> showRegion() {

//      List<ShowRegionVO> showRegions = showRegionMapper.selectByGrandchildren();
       List<ShowRegionVO> showRegionVOS = showRegionMapper.showRegion();

        return  showRegionVOS;
    }

    /**
     * 品牌制造商
     * @param brandInfoBO
     * @return
     */
    @Override
    public BrandInfoVO getBrandInfo(BrandInfoBO brandInfoBO) {
        //分页功能
        PageHelper.startPage(brandInfoBO.getPage(), brandInfoBO.getLimit());

        List<BrandInfo> brandInfos1 = brandInfoMapper.getBrandInfo(brandInfoBO);
        //这是返回的VO对象
        Integer total = brandInfoMapper.getTotal(brandInfoBO);
        BrandInfoVO brandInfoVO = new BrandInfoVO();

        brandInfoVO.setItems(brandInfos1);
        brandInfoVO.setTotal(total);
        return brandInfoVO;
    }

    @Override
    public CreateBandVO createBrand(CreateBrandBO createBrandBO) {
        //先把前端数据放进去  再从数据库全查出来
        Date date = new Date();
        BrandInfo brandInfo = new BrandInfo();
        brandInfo.setAddTime(date);
        brandInfo.setUpdateTime(date);
        brandInfo.setName(createBrandBO.getName());
        brandInfo.setDesc(createBrandBO.getDesc());
        brandInfo.setPicUrl(createBrandBO.getPicUrl());
        brandInfo.setFloorPrice(createBrandBO.getFloorPrice());

        brandInfoMapper.insertCreateInfo(brandInfo);
        CreateBandVO createBandVO = brandInfoMapper.selectCreateInfo(brandInfo.getId());
        return createBandVO;
    }

    @Override
    public UpdateBrandVO updateBrand(CreateBrandBO createBrandBO) {
        Date date = new Date();
        BrandInfo brandInfo = new BrandInfo();
        brandInfo.setName(createBrandBO.getName());
        brandInfo.setDesc(createBrandBO.getDesc());
        brandInfo.setPicUrl(createBrandBO.getPicUrl());
        brandInfo.setFloorPrice(createBrandBO.getFloorPrice());
        brandInfo.setUpdateTime(date);
        brandInfo.setId(createBrandBO.getId());
        brandInfoMapper.updateBrandInfo(brandInfo);

        UpdateBrandVO updateBrandVO = brandInfoMapper.selectUpdateInfo(createBrandBO.getId());
        return updateBrandVO;
    }

    @Override
    public void deleteBrand(Integer id) {
        brandInfoMapper.deleteBrand(id);
    }
}
